home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Applications / QuArK / plugins / mapfindreptex.py < prev    next >
Text File  |  2004-01-05  |  8KB  |  261 lines

  1. #-------------------------------------------------------------------------------
  2. #
  3. #       Module:         mapfindreptex.py
  4. #       Subsystem:      mapfindreptex
  5. #       Program:        mapfindreptex
  6. #
  7. #       Copyright (c) 1998 - Descartes Systems Sciences
  8. #
  9. #-------------------------------------------------------------------------------
  10. #
  11. #       Description:
  12. #
  13. #-------------------------------------------------------------------------------
  14. #
  15. #       $History: mapfindreptex $
  16. #
  17. #-------------------------------------------------------------------------------
  18.  
  19. #$Header: /cvsroot/quark/runtime/plugins/mapfindreptex.py,v 1.9 2003/03/21 05:47:45 cdunde Exp $
  20.  
  21.  
  22.  
  23. Info = {
  24.    "plug-in":       "Search and replace textures",
  25.    "desc":          "Search/replace textures dialog box, available from the Search menu.",
  26.    "date":          "17 oct 98",
  27.    "author":        "Tim Smith & Armin Rigo",
  28.    "author e-mail": "",
  29.    "quark":         "Version 5.0.c5" }
  30.  
  31.  
  32. import quarkx
  33. import quarkpy.qmacro
  34. import quarkpy.qtoolbar
  35. import quarkpy.mapsearch
  36. from quarkpy.maputils import *
  37.  
  38. class SearchReplaceTextureDlg(quarkpy.qmacro.dialogbox):
  39.  
  40.     #
  41.     # dialog layout
  42.     #
  43.  
  44.     size = (300, 240)
  45.     dfsep = 0.4        # separation at 40% between labels and edit boxes
  46.     dlgflags = FWF_KEEPFOCUS
  47.  
  48.     dlgdef = """
  49.       {
  50.         Style = "15"
  51.         Caption = "Search/replace textures"
  52.         sep: = {Typ="S" Txt="By leaving the 'Replace with texture' blank or"}
  53.         sep: = {Typ="S" Txt="equal to the 'Search for texture', this will"}
  54.         sep: = {Typ="S" Txt="only perform a search-and-select-objects."}
  55.         sep: = {Typ="S" Txt=" "}
  56.         fromtex: = {
  57.           Txt = "Search for texture:"
  58.           Typ = "ET"
  59.           SelectMe = "1"
  60.         }
  61.         totex: = {
  62.           Txt = "Replace with texture:"
  63.           Typ = "ET"
  64.         }
  65.         scope: = {
  66.           Typ = "CL"
  67.           Txt = "Search in:"
  68.           Items = "%s"
  69.           Values = "%s"
  70.         }
  71.         sep: = {Typ="S" Txt=" "}
  72.         ReplaceAll:py = {Txt=""}
  73.         close:py = {Txt=""}
  74.       }
  75.     """
  76.  
  77.     #
  78.     # __init__ initialize the object
  79.     #
  80.  
  81.     def __init__(self, form, editor):
  82.  
  83.         #
  84.         # General initialization of some local values
  85.         #
  86.  
  87.         self.editor = editor
  88.         uniquesel = editor.layout.explorer.uniquesel
  89.         self.sellist = self.editor.visualselection()
  90.  
  91.         #
  92.         # Create the data source
  93.         #
  94.  
  95.         src = quarkx.newobj(":")
  96.  
  97.         #
  98.         # Based on the textures in the selections, initialize the
  99.         # from and to textures
  100.         #
  101.  
  102.         if len(self.sellist) == 0:
  103.             texlist = quarkx.texturesof(editor.Root.findallsubitems("", ':f'))
  104.         elif uniquesel is not None and uniquesel.type==":f":
  105.             texlist = quarkx.texturesof([uniquesel])
  106.         else:
  107.             texlist = quarkx.texturesof(self.sellist);
  108.         if len(texlist) == 0:
  109.             texlist.append(quarkx.setupsubset()["DefaultTexture"])
  110.         src["fromtex"] = texlist[0]
  111.         src["totex"] = texlist[0]
  112.  
  113.         #
  114.         # Based on the selection, populate the range combo box
  115.         #
  116.  
  117.         if len(self.sellist) == 0:
  118.             src["scope"] = "W"
  119.             src["scope$Items"] = "Whole map"
  120.             src["scope$Values"] = "W"
  121.         else:
  122.             src["scope"] = "S"
  123.             src["scope$Items"] = "Selection\nWhole map"
  124.             src["scope$Values"] = "S\nW"
  125.  
  126.         #
  127.         # Create the dialog form and the buttons
  128.         #
  129.  
  130.         quarkpy.qmacro.dialogbox.__init__(self, form, src,
  131.            close      = quarkpy.qtoolbar.button(self.close, "Close this box", ico_editor, 0, "Close", 1),
  132.            ReplaceAll = quarkpy.qtoolbar.button(self.ReplaceAll, "Search/replace textures", ico_editor, 2, "Search/replace", 1)
  133.         )
  134.  
  135.  
  136.     def deepsearch(self, objs, find, newsellist):
  137.         # Performs a recursive search of ':f' objects, and appends them to a list
  138.         for o in objs:
  139.             if o.type == ':f':
  140.                 if o.texturename == find:
  141.                     newsellist.append(o)
  142.             else:
  143.                 if len(o.subitems) > 0:
  144.                     self.deepsearch(o.subitems, find, newsellist)
  145.  
  146.     def ReplaceAll(self, btn):
  147.  
  148.         #
  149.         # commit any pending changes in the dialog box
  150.         #
  151.  
  152.         quarkx.globalaccept()
  153.  
  154.         #
  155.         # read back data from the dialog box
  156.         #
  157.  
  158.         whole = self.src["scope"] == "W"
  159.         find = self.src["fromtex"]
  160.         replace = self.src["totex"]
  161.         if not find:
  162.             quarkx.msgbox("Please specify texture name to search for.", MT_ERROR, MB_OK)
  163.             return
  164.  
  165.         list = None
  166.         if whole:
  167.             list = self.editor.Root.findallsubitems("", ':f')
  168.         else:
  169.             list = self.sellist
  170.  
  171.         if not replace or replace == find:
  172.             # No replace texture-name have been given. Then it must just be a "Search" the user intended.
  173.             newsellist = []
  174.             self.deepsearch(list, find, newsellist)
  175.  
  176.             # Set views to the new selection
  177.             self.editor.layout.explorer.sellist = newsellist
  178.  
  179.             # Notify the user, we're finished searching
  180.             txt = "%d faces selected" % len(newsellist)
  181.             result = quarkx.msgbox(txt, MT_INFORMATION, MB_OK)
  182.  
  183.             # Close the search-dialogbox
  184.             self.close()
  185.         else:
  186.             #
  187.             # do the changes
  188.             #
  189.             changes = 0
  190.             undo = quarkx.action()
  191.  
  192.             for o in list: # loop through the list
  193.                 changes = changes + o.replacetex(find, replace, 1)
  194.             txt = None
  195.             if changes:
  196.                 txt = "%d textures replaced" % changes
  197.                 mb = MB_OK_CANCEL
  198.             else:
  199.                 txt = "No textures replaced"
  200.                 mb = MB_CANCEL
  201.             result = quarkx.msgbox(txt, MT_INFORMATION, mb)
  202.  
  203.             #
  204.             # commit or cancel the undo action
  205.             #
  206.  
  207.             if result == MR_OK:
  208.                 undo.ok(self.editor.Root, "replace textures")   # note: calling undo.ok() when nothing has actually been done is the same as calling undo.cancel()
  209.                 #
  210.                 # Sorry, we have to close the dialog box, because the selection changed.
  211.                 # Allowing the user to make multiple replacements in the selection before committing them all
  212.                 #  would be a bit more complicated.
  213.                 #
  214.                 self.close()
  215.             else:
  216.                 undo.cancel()
  217.  
  218. #
  219. # Function to start the replace dialog
  220. #
  221.  
  222. def SearchReplaceTexClick(m):
  223.     editor = mapeditor()
  224.  
  225.     if editor is None:
  226.         return
  227.     SearchReplaceTextureDlg(quarkx.clickform, editor)
  228.  
  229. #
  230. # Register the replace texture menu item
  231. #
  232.  
  233. quarkpy.mapsearch.items.append(quarkpy.qmenu.item("Search/replace textures...", SearchReplaceTexClick, "|Search/replace textures:\n\nThis function can either just search for a particular texture, or\nit can replace it with another texture of your choice.", "intro.mapeditor.menu.html#searchmenu"))
  234.  
  235.  
  236. # ----------- REVISION HISTORY ------------
  237. # $Log: mapfindreptex.py,v $
  238. # Revision 1.9  2003/03/21 05:47:45  cdunde
  239. # Update infobase and add links
  240. #
  241. # Revision 1.8  2002/06/10 09:27:39  decker_dk
  242. # Updated to allow only search-for-texture, without replacing any.
  243. # TODO: Switch to specify search (and replace) for brushes that have the texture either on _all_ its faces _or_ only partly.
  244. #
  245. # Revision 1.7  2002/05/16 03:05:41  tiglari
  246. # If just a face is selected, the texture on the face is loaded into the dialog
  247. #  (bug report from fpbrowser)
  248. #
  249. # Revision 1.6  2002/04/07 12:46:06  decker_dk
  250. # Pretty separator.
  251. #
  252. # Revision 1.5  2001/06/17 21:21:18  tiglari
  253. # re-fix button captions, there are tabs in this file, need to be cleared out
  254. #
  255. # Revision 1.3  2001/01/27 18:25:29  decker_dk
  256. # Renamed 'TextureDef' -> 'DefaultTexture'
  257. #
  258. # Revision 1.2  2000/06/03 10:25:30  alexander
  259. # added cvs headers
  260. #
  261.